package study.JavaBase;

import java.util.Arrays;

/**
 * date:2024.10.13
 * title:java数组
 * author:fzy
 */
public class Java_12 {
    public static void main(String[] args) {
//        ArrayClass.arrayOperation();
        //多个数组指向相同地址
        //修改任意一个数组的值，两个数组都会改变
        int[] int_array1 = new int[3];
        int_array1[0] = 100;
        int_array1[1] = 200;
        int_array1[2] = 300;
        int[] int_array2 = int_array1;
        int_array2[2] = 400;
        for (int i = 0;i<int_array1.length;i++){
            System.out.println("数组一:"+int_array1[i]);
            System.out.println("数组二:"+int_array2[i]);
        }
        int_array1[1]=40000;
        for (int i = 0;i<int_array1.length;i++){
            System.out.println("数组一:"+int_array1[i]);
            System.out.println("数组二:"+int_array2[i]);
        }


    }
}

/**
 * java语言用来存储固定大小的同类型数据
 */
class ArrayClass {
    static void arrayOperation() {
        //动态初始化
        int[] int_array = new int[3];
        //静态初始化
        double[] dAr = {1d, 2d, 3d};
        //构造函数创建数组
        double[] dAr2 = new double[10];
        for (int i = 0; i < dAr2.length; i++) {
            dAr2[i] = (double) i;
        }
        System.out.println("dAr2: " + Arrays.toString(dAr2));
        //操作数组-循环
        for (double d : dAr2) {
            System.out.println(d);
        }
        int[] intA = {322, 1, 2, 1, 12, 51, 123};
        System.out.println("max: " + ArrayClass.findMaxInArr(intA));
        //多维数组
        int[][] twoAr = {{1, 2, 3}, {3, 4}};
        float[][] fAr = new float[2][];
        fAr[0] = new float[2];
        fAr[1] = new float[3];
        fAr[0][1] = 20.1231f;
        System.out.println(fAr[0][1]);

    }


    static int findMaxInArr(int[] arr) {
        int max = 0;
        for (int i : arr) {
            if (i > max) {
                max = i;
            }
        }
        return max;
    }

    //Arrays类
    static void arraysFun(){
        int[] iA = new int[10];
        //把32赋给iA所有元素
        Arrays.fill(iA, 32);
        //排序
         Arrays.sort(iA);
         //对比
        long[] lA = new long[10];
        long[] lV = new long[10];
        Arrays.fill(lA, 32);
        Arrays.fill(lV,12);
        boolean a  = Arrays.equals(lA,lV);

    }
}

/*
java内存分配
数组在初始化时，默认为存储空间存值，整数默认0，浮点数默认0.0，bool默认false，引用数据默认null
 */